﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Xushre.WeiBoSDK {

    public partial class OClient2 {

        /// <summary>
        /// 获取当前用户的微博标签列表
        /// </summary>
        /// <param name="count">返回结果的条数数量，最大不超过200，默认为20。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string StatusesTags(int count = 20) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("count", count);

            try {
                response = Get(API.OClient2.StatusesTags.TAGS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 根据提供的微博ID批量获取此微博的标签信息
        /// </summary>
        /// <param name="id">根据指定id返回微博的相应标签信息。一次最多传入50个id。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string StatusesTagsShowBatch(Int64[] id) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();
            string ids = string.Empty;

            if (id.Length > 50) {
                return "{\"error\":\"Parameter must be less than the number of id 50.\",\"error_code\":9014}";
            }
            ids = String.Join(",", id);

            data.Add("ids", ids);

            try {
                response = Get(API.OClient2.StatusesTags.TAGS_SHOW_BATCH, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取当前用户某个标签的微博ID列表
        /// </summary>
        /// <param name="tag">要获取的标签。</param>
        /// <param name="since_id">若指定此参数，则只返回ID比since_id大的微博消息（即比since_id发表时间晚的微博消息），默认为0。</param>
        /// <param name="max_id">若指定此参数，则返回ID小于或等于max_id的微博消息，默认为0。</param>
        /// <param name="count">返回结果的条数数量，最大不超过200，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string StatusesTagTimeline(string tag, Int64 since_id = 0, Int64 max_id = 0, int count = 50, int page = 1) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("tag", tag);
            data.Add("since_id", since_id);
            data.Add("max_id", max_id);
            data.Add("count", count);
            data.Add("page", page);

            try {
                response = Get(API.OClient2.StatusesTags.TAG_TIMELINE, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 创建当前登陆用户微博的标签
        /// </summary>
        /// <remarks>用户微博标签总数不能超过200个，超过时再添加会返回“20117” too many错误。</remarks>
        /// <param name="tag">要创建标签名字。长度不可超过7个汉字，14个半角字符，不能包含空格。程序将自动去除空格及截取长度</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string StatusesTagsCreate(string tag) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            tag = tag.Replace(" ", "");
            if (Encoding.Default.GetByteCount(tag) > 14) {
                tag = Tools.isSingle(tag) ? tag.Substring(0, 7) : tag.Substring(0, 14);
            }

            data.Add("tag", tag);

            try {
                response = Post(API.OClient2.StatusesTags.TAGS_CREATE, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 删除当前登陆用户微博的标签
        /// </summary>
        /// <param name="tag">要删除的标签名字。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string StatusesTagsDestory(string tag) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("tag", tag);

            try {
                response = Post(API.OClient2.StatusesTags.TAGS_DESTORY, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 更新当前登陆用户微博的标签
        /// </summary>
        /// <param name="old_tag">用户微博的旧标签。</param>
        /// <param name="new_tag">用户微博的新标签。长度不可超过7个汉字，14个半角字符，不能包含空格。程序将自动去除空格及截取长度</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string StatusesTagsUpdate(string old_tag, string new_tag) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            new_tag = new_tag.Replace(" ", "");
            if (Encoding.Default.GetByteCount(new_tag) > 14) {
                new_tag = Tools.isSingle(new_tag) ? new_tag.Substring(0, 7) : new_tag.Substring(0, 14);
            }

            data.Add("old_tag", old_tag);
            data.Add("new_tag", new_tag);

            try {
                response = Post(API.OClient2.StatusesTags.TAGS_UPDATE, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 更新当前登陆用户某个微博的标签
        /// </summary>
        /// <remarks>tags为空或指定了不存在的标签，即为删除该微博的标签。</remarks>
        /// <param name="id">微博ID。</param>
        /// <param name="tags">需要修改成的标签内容。最多5条，逗号分隔。程序自动进行编码处理</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string StatusesUpdateTags(Int64 id, string[] tag = null) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            string tags = string.Empty;
            if (tag != null && tag.Length > 0) {
                string[] temp = new string[5];
                for (int count = 0, i = 0; count < 5 && i < tag.Length; i++) {
                    temp[count] = System.Web.HttpUtility.UrlEncode(Encoding.UTF8.GetString(Encoding.Default.GetBytes(tag[i])));
                    count++;
                }

                tags = String.Join(",", temp);
            }

            data.Add("id", id);
            data.Add("tags", tags);

            try {
                response = Post(API.OClient2.StatusesTags.UPDATE_TAGS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }
    }
}
